﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Huinn.Common.Cache
{
    class CacheHelper
    {
        public static void SetCache(Object content, string cacheKey, double absoluteCacheTimeSpan)
        {
            if (System.Web.HttpRuntime.Cache[cacheKey] == null)
            {
                //add cache
                System.Web.HttpRuntime.Cache.Add(cacheKey,
                       content,
                       null,
                       DateTime.Now.AddMinutes(absoluteCacheTimeSpan),
                       System.Web.Caching.Cache.NoSlidingExpiration,
                       System.Web.Caching.CacheItemPriority.High, null);
            }
            else
            {
                //remove cache
                System.Web.HttpRuntime.Cache.Remove(cacheKey);

                //add cache
                System.Web.HttpRuntime.Cache.Add(cacheKey,
                       content,
                       null,
                       DateTime.Now.AddMinutes(absoluteCacheTimeSpan),
                        System.Web.Caching.Cache.NoSlidingExpiration,
                       System.Web.Caching.CacheItemPriority.High, null);
            }
        }

        public static void SetCache(Object content, string cacheKey, int relativeCacheTimeSpan)
        {
            if (System.Web.HttpRuntime.Cache[cacheKey] == null)
            {
                //add cache
                System.Web.HttpRuntime.Cache.Add(cacheKey,
                       content,
                       null,
                       DateTime.MaxValue,
                      TimeSpan.FromMinutes(relativeCacheTimeSpan),
                       System.Web.Caching.CacheItemPriority.High, null);
            }
            else
            {
                //remove cache
                System.Web.HttpRuntime.Cache.Remove(cacheKey);

                //add cache
                System.Web.HttpRuntime.Cache.Add(cacheKey,
                       content,
                       null,
                       DateTime.MaxValue,
                       TimeSpan.FromMinutes(relativeCacheTimeSpan),
                       System.Web.Caching.CacheItemPriority.High, null);
            }
        }

        public static Object GetCache(string cacheKey)
        {
            if (System.Web.HttpRuntime.Cache[cacheKey] != null)
                return System.Web.HttpRuntime.Cache[cacheKey];
            return null;
        }
    }
}
